package portal.monitor.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;

import portal.common.MesageCode;
import portal.common.MyStringUtils;
import portal.common.ResultData;
import portal.monitor.cmd.ExcuCmd;
import portal.monitor.cmd.ScheCmd;
import portal.monitor.service.HostCtrlService;
import portal.monitor.utils.MonBaseUtils;
import portal.monitor.utils.MonConfig;
import utils.Log4jInit;

@Service("hostCtrlService")
public class HostCtrlServiceImpl implements HostCtrlService {


	@Override
	public ResultData readScript(String scripType) {
		// TODO Auto-generated method stub
		
		String rootPath = MonConfig.localBase+scripType;
		File dir = new File(rootPath);
		ResultData rd = new ResultData();
		if(!dir.exists() || (!dir.isDirectory())){
			dir .mkdirs();
			rd.resultCode = MesageCode.ERROR;
			rd.data = "读取失败，文件夹为空";
		}
		String[] files = dir.list();
		JSONArray arr = new JSONArray();
		arr.add(files);
		rd.resultCode = MesageCode.SUCCESS;
//		rd.data = arr.toJSONString();
		rd.data = files;
		return rd;
	}

	@Override
	public ResultData postFile(String[] IPS, String filepath,boolean deploy) {
		// TODO Auto-generated method stub
		
		ResultData rd = new ResultData();
		for(String ip :IPS){
			String rt =ExcuCmd.download_deploy(filepath, ip,deploy);
			if(!rt.equals("download file success")){
				rd.resultCode = rd.resultCode+","+MesageCode.ERROR;
				rd.data = ip+" upload file error:"+rt;
				continue;
			}
			rd.resultCode = rd.resultCode+","+MesageCode.SUCCESS;
			rd.data = rd.data+"  upload success!";
		}
		return rd;
	}

	

	@Override
	public ResultData execCmd(String IP,String type) {
		// TODO Auto-generated method stub
		String cmd = MonConfig.remoteBase+"GIS.exe ";
		ResultData rd = new ResultData();
		if(type.equals("process")){
			cmd = cmd+" 2";
			String command = MonConfig.remoteExecPath+MonConfig.fileclient + " -m " + MonConfig.localhost + " -l " + MonConfig.remoteTxtPath +"PL.txt" + " -r " +MonConfig.localBase+"PL.txt"
					+ " -d -p 6522 -n jhadmin";
			String upload = ExcuCmd.execclient3(command,IP);	
			if(!upload.contains("success")){
				rd.resultCode = MesageCode.ERROR;
				rd.data = "PL文件上传失败";
				return rd;
			}
		}else if(type.equals("app")){
			cmd = cmd+" 1";
		}else if(type.equals("mac")){
			cmd = cmd+" 4";
		}else if(type.equals("usb")){
			cmd = MonConfig.remoteBase+"getUsbInfo.exe";
		}
		
		String rt =ExcuCmd.execclient3(cmd,IP);
	
		if(MyStringUtils.isEmpty(rt)){
			rd.resultCode = MesageCode.ERROR;
			rd.data = "无查询结果。";
		}else{
			rd.resultCode = MesageCode.SUCCESS;
				rd.data = rt;
		}
		
		return rd;
	}

	@Override
	public ResultData shutdown(String[] IPS) {
		// TODO Auto-generated method stub
//		String[] cmd = {"shutdown","-s"};
		String cmd = "shutdown -s";
		for(String ip:IPS){
			ExcuCmd.execclient3(cmd, ip);
		}
		ResultData rt = new ResultData();
		return rt;
	}
	
	@Override
	public ResultData removeFiles(String IPS,String files){
		
		ResultData rt = new ResultData();
		String[] tmp = files.split(",");
		String filesStr = "";
		for(int k = 0;k<tmp.length;k++){
			filesStr += " \""+MonConfig.remoteBase +tmp[k] +"\"";
		}
		filesStr =filesStr.replaceAll("/", "\\\\");
		
		Log4jInit.logger.info("-----------"+tmp.length);
		Log4jInit.logger.info("-----------del files: "+filesStr);
		String cmd = "del " + filesStr;
		String folder =  MonConfig.localBase+"script/";
		String fileName = "delete.bat";
		
		File batFile = MonBaseUtils.createFile(cmd, folder,fileName);
		
		if(!batFile.exists()){
			rt.resultCode = MesageCode.ERROR;
			rt.desc = "创建bat文件失败";
			return rt;
		}
		Log4jInit.logger.info("=========="+batFile.getAbsolutePath());
		if(!MonConfig.useCopy){
			List<String> fileList = new ArrayList<String>();
			try {
				MonBaseUtils.updateState(folder, MonBaseUtils.generateScriptFile(folder+fileName, folder+fileName,ScheCmd.class.getResource("/"+MonConfig.execclientUri).getPath()), fileList);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		rt =ScheCmd.excuteSched(folder+fileName, IPS, batFile.getAbsolutePath(), "DELFILE");
		
		return rt;
	}
	
	
	/*public String cmdThreadCall(String method,Map<String,Object> param){
		
		ExecutorService exec = Executors.newCachedThreadPool();
		Future<String> future = exec.submit(new ExcuCmd(method,param));
		
		
		
		return null;
	}
*/
	
}
